#    One Room And A Goblin Invasion
#    Copyright (C) 2014  Jannik Haberbosch
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.


# Python modules
import copy

# 3rd party modules
import pygame
pygame.init()

# Project modules
from code import helpers

# Module constants. FIXME:  Not happy about this.
ANIMATION_PING_PONG = 1
ANIMATION_ONCE = 2


class AnimatedSprite( pygame.sprite.Sprite ):
	"""This class represents an animated sprite.
	
	"""
	def __init__( self,
		imagelist,
		position_center,
		timer,
		order = ANIMATION_PING_PONG,
		initial_angle = 0 ):
		"""Constructor.

		Parameters
			imagelist
			list
			A list that contains strings of which each defines the filename of
			an acutal image to use for the animation.
			
			position_center
			list or tuple
			This consists of two integer elements [x, y] that define the center
			of the animation.
			
			timer
			Timer
			A Timer instance that defines how fast the animation runs.
			
			order
			integer
			An integer value which defines how an animation is run (ping-pong,
			restart,...) TODO!

			initial_angle
			integer
			A value between 1 - 350 (and including) defining the initial angle
			of the animation.  The images will all be rotated to match this
			angle.  Default is 0.
		"""
		# First the base-class constructor has to be called.
		pygame.sprite.Sprite.__init__( self )

		self.imagelist = imagelist
		self.position_center = position_center
		self.timer = timer
		self.order = order
		self.initial_angle = initial_angle

		# Now the images are loaded.
		self.images = self.load_images()

		# Original images are required to rotate the images later on and not
		# lose any quality.
		self.original_images = self.load_images()

		# The first image is displayed first.
		self.currently_chosen_image_index = 0

		# How many images are there in total?
		self.n_images = len( self.images )

		# The default angle is 0. (showing to the right of the screen).
		self.angle = initial_angle

		# Rotate all images to match the initial angle.
		self.rotate_images( self.initial_angle )

		# Now the image and rect are set.
		self.set_next_image()


	def update( self ):
		"""Updates the animation.

		"""
		# Check whether the next image should be chosen and shown.
		if self.timer.update():

			# If true, the imagelist is reversed and the animation continues.
			if self.n_images == ( self.currently_chosen_image_index + 1 ) and \
				self.order == ANIMATION_PING_PONG:
				self.currently_chosen_image_index = 0
				self.images.reverse()
				self.original_images.reverse()

			elif self.n_images == ( self.currently_chosen_image_index + 1 ) and \
				self.order == ANIMATION_ONCE:
				return True
				
			else:
				self.currently_chosen_image_index += 1

			# The next image will now be chosen and set.
			self.set_next_image()


	def set_next_image( self ):
		"""Sets the next image.

		"""
		self.image = self.images[self.currently_chosen_image_index]
		self.rect = self.image.get_rect()
		self.rect.center = self.position_center


	def load_images( self ):
		"""Loads images to use for the animation.

		Returns
			images
			list
			A list that contains pygame.Surface objects.

		"""
		images = []

		for filename in self.imagelist:
			images.append(
				helpers.load_image(
					filename
				)
			)

		return images


	def rotate_images( self, degree = 1 ):
		"""This function rotates all images.

		Parameters
			degree
			integer
			Describes by how much to rotate an image

		"""
		self.angle += degree
		if self.angle >= 360:
			self.angle = 0
		if self.angle < 0:
			self.angle = 359

		for x in range( len( self.images ) ):
			new_image = pygame.transform.rotate( self.original_images[x], self.angle )
			self.images[x] = new_image
